// AMD 模式理解，define声明模块，require引入模块


//模拟实现require
var factories = {};
function define(moduleName,dependencies,factory){
   factories[moduleName] = factory;
   factory.dependencies = dependencies;
}
function require(mods,callback){
  let result = mods.map(function(mod){
      let factory = factories[mod];
      let exports;
      let dependencies = factory.dependencies;
      require(dependencies,function(){
         exports = factory.apply(null,arguments)
      })
      return exports;
  })
  callback.apply(null,result);
}

// 第一种写法：
define('name',[],function(){
  return 'trump'
})

define('age',[],function(){
  return 45;
})

require(['name','age'],function(name,age){
  console.log(name,age)
})

// 第二种写法：
define('name',[],function(){
  return 'trump'
})

define('age',['name'],function(name){
  return name+45;
})

require(['age'],function(age){
  console.log(age)
})

//模拟实现commonjs
//比如我们有一个 index.js 文件，依赖了 module1 和 module2 两个模块，并且 module1 依赖 module2 ：
//index.js
var module1 = require("./module1");
var module2 = require("./module2");

module1.foo();
module2.foo();

function hello(){
    console.log("Hello!");
}

module.exports = hello;
//module1.js
var module2 = require(module2);
console.log("initialize module1");

console.log("this is module2.foo() in module1:");
module2.foo();
console.log("\n")

module.exports = {
    foo: function(){
        console.log("module1 foo !!!");
    }
};
//module2.js
console.log("initialize module2");

module.exports = {
    foo: function(){
        console.log("module2 foo !!!");
    }
};
//commonJs模拟：
(function(modules) {
  var installedModules = {};

  function require(moduleName) {
      //如果模块已经导入，那么直接返回它的exports
      if (installedModules[moduleName]) {
          return installedModules[moduleName].exports;
      }

      //模块初始化
      var module = installedModules[moduleName] = {
          exports: {},
          name: moduleName,
          loaded: false
      };

      //执行模块内部的代码，这里的 modules 变量即为我们在上面写好的 modules 对象
      modules[moduleName].call(module.exports, module, module.exports, require);
      //模块导入完成
      module.loaded = true;
      //将模块的exports返回
      return module.exports;
  }
  return require("entry");
})({
  "entry": function(module, exports, require, global) {
    //index.js
    var module1 = require("./module1");
    var module2 = require("./module2");
    module1.foo();
    module2.foo();

    function hello() {
        console.log("Hello!");
    }

    module.exports = hello;
},

"./module1": function(module, exports, require, global) {
    var module2 = require("./module2");
    console.log("initialize module1");

    console.log("this is module2.foo() in module1:");
    module2.foo();
    console.log("\n")

    module.exports = {
        foo: function() {
            console.log("module1 foo !!!");
        }
    };
},
"./module2": function(module, exports, require, global) {
  console.log("initialize module2");
  module.exports = {
      foo: function() {
          console.log("module2 foo !!!");
      }
  };
}
});